import 'package:flutter/material.dart';
import '../../core/errors/error_handler.dart';

/// 统一的错误展示组件
class AppErrorWidget extends StatelessWidget {
  final dynamic error;
  final VoidCallback? onRetry;
  final String? title;

  const AppErrorWidget({
    super.key,
    required this.error,
    this.onRetry,
    this.title,
  });

  @override
  Widget build(BuildContext context) {
    final errorMessage = ErrorHandler.getErrorMessage(error);
    final isRetryable = ErrorHandler.isRetryable(error);

    return Center(
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              _getErrorIcon(),
              size: 64,
              color: Colors.grey,
            ),
            const SizedBox(height: 16),
            Text(
              title ?? '出错了',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 8),
            Text(
              errorMessage,
              textAlign: TextAlign.center,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                    color: Colors.grey[600],
                  ),
            ),
            if (isRetryable && onRetry != null) ...[
              const SizedBox(height: 24),
              ElevatedButton.icon(
                onPressed: onRetry,
                icon: const Icon(Icons.refresh),
                label: const Text('重试'),
              ),
            ],
          ],
        ),
      ),
    );
  }

  IconData _getErrorIcon() {
    if (ErrorHandler.isNetworkError(error)) {
      return Icons.wifi_off;
    } else if (ErrorHandler.isAuthError(error)) {
      return Icons.lock_outline;
    } else {
      return Icons.error_outline;
    }
  }
}

/// 错误 SnackBar 展示
class ErrorSnackBar {
  static void show(
    BuildContext context,
    dynamic error, {
    Duration duration = const Duration(seconds: 3),
  }) {
    final message = ErrorHandler.getErrorMessage(error);
    
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Row(
          children: [
            const Icon(Icons.error_outline, color: Colors.white),
            const SizedBox(width: 8),
            Expanded(
              child: Text(message),
            ),
          ],
        ),
        backgroundColor: Colors.red,
        duration: duration,
        behavior: SnackBarBehavior.floating,
      ),
    );
  }
}

/// 错误 Dialog 展示
class ErrorDialog {
  static Future<void> show(
    BuildContext context,
    dynamic error, {
    String? title,
    VoidCallback? onConfirm,
  }) {
    final message = ErrorHandler.getErrorMessage(error);
    
    return showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(title ?? '错误'),
        content: Text(message),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.of(context).pop();
              onConfirm?.call();
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }
}

